package cn.hsp.tiktok

import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.fragment.app.Fragment
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.ImmersionBar
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity() {
    private var lastIndex = 0
    private var mFragments = mutableListOf<Fragment>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initBottomNavigation()
        initData()
        //沉浸式全屏
        ImmersionBar.with(this).init()
    }

    private fun initData() {
        mFragments = ArrayList()
        mFragments.add(HomeVideoListFragment())
        setFragmentPosition(0)
    }

    private fun initBottomNavigation() {
        tabMainMenu.addTab(tabMainMenu.newTab().setText("首页"))
        tabMainMenu.addTab(tabMainMenu.newTab().setText("朋友"))
        tabMainMenu.addTab(tabMainMenu.newTab().setText(""))
        tabMainMenu.addTab(tabMainMenu.newTab().setText("消息"))
        tabMainMenu.addTab(tabMainMenu.newTab().setText("我"))
    }

    private fun setFragmentPosition(position: Int) {
        val ft = supportFragmentManager.beginTransaction()
        val currentFragment = mFragments[position]
        val lastFragment = mFragments[lastIndex]
        lastIndex = position
        ft.hide(lastFragment)
        if (!currentFragment.isAdded) {
            supportFragmentManager.beginTransaction().remove(currentFragment).commit()
            ft.add(R.id.ll_frameLayout, currentFragment)
        }
        ft.show(currentFragment)
        ft.commitAllowingStateLoss()
    }
}